package com.tianxiao.faas.biz.service.corner;

import com.tianxiao.faas.api.param.corner.*;
import com.tianxiao.faas.common.util.DateUtils;
import com.tianxiao.faas.common.util.LikeQueryUtils;
import com.tianxiao.faas.common.util.PageUtils;
import com.tianxiao.faas.mapper.dao.SearchHistoryModelMapper;
import com.tianxiao.faas.mapper.model.ActivityModel;
import com.tianxiao.faas.mapper.model.CustomInfoModel;
import com.tianxiao.faas.mapper.model.SearchHistoryModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SearchHistoryService {

    @Resource
    private SearchHistoryModelMapper searchHistoryModelMapper;

    public void save(Long userId, String searchName, Integer type) {
        long currentTimeMillis = System.currentTimeMillis();
        SearchHistoryModel searchHistoryModel1 = searchHistoryModelMapper.selectByName(userId, searchName, type);
        SearchHistoryModel searchHistoryModel = new SearchHistoryModel();
        if (searchHistoryModel1 != null) {
            searchHistoryModel.setId(searchHistoryModel1.getId());
            searchHistoryModel.setUpdated(currentTimeMillis);
            searchHistoryModelMapper.updateByPrimaryKeySelective(searchHistoryModel);
        } else {
            searchHistoryModel.setUserId(userId);
            searchHistoryModel.setSearchName(searchName);
            searchHistoryModel.setType(type);
            searchHistoryModel.setCreated(currentTimeMillis);
            searchHistoryModel.setUpdated(currentTimeMillis);
            searchHistoryModel.setDeleted(0);
            searchHistoryModelMapper.insertSelective(searchHistoryModel);
        }

    }

    public void delete(Long id) {
        SearchHistoryModel searchHistoryModel = new SearchHistoryModel();
        searchHistoryModel.setId(id);
        searchHistoryModel.setDeleted(1);
        searchHistoryModelMapper.updateByPrimaryKeySelective(searchHistoryModel);
    }

    public void deleteAll(Long userId, Integer type) {
        long currentTimeMillis = System.currentTimeMillis();
        searchHistoryModelMapper.updateIsDeletedByUserId(userId, currentTimeMillis, type);
    }

    public List<SearchHistoryResult> list(Long userId, Integer type) {
        Integer count = 10;
        if (type == 2) {
            count = 5;
        }
        List<SearchHistoryModel> searchHistoryModels = searchHistoryModelMapper.list(userId, type, count);
        List<SearchHistoryResult> searchHistoryResults = new ArrayList<>();
        for (SearchHistoryModel searchHistoryModel : searchHistoryModels) {
            SearchHistoryResult searchHistoryResult = getSearchHistoryResult(searchHistoryModel);
            searchHistoryResults.add(searchHistoryResult);
        }
        return searchHistoryResults;
    }


    private SearchHistoryResult getSearchHistoryResult(SearchHistoryModel searchHistoryModel) {
        SearchHistoryResult searchHistoryResult = new SearchHistoryResult();
        searchHistoryResult.setId(searchHistoryModel.getId());
        searchHistoryResult.setSearchName(searchHistoryModel.getSearchName());
        return searchHistoryResult;
    }
}
